import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/x3_component_core.dart';
import 'package:x3_form_core/components/base_form_another_radio.dart';
import 'package:x3_form_core/provider/base_request_form_provider.dart';
import 'package:x3_form_core/business_components/base_request_form_container.dart';

class BaseRequestFormAnOtherRadio extends StatefulWidget {
  BaseRequestFormAnOtherRadio({Key key, @required this.request, this.requestSaveData}) : super(key: key);

  final BaseHttpRequestModel request;

  final RequestSaveData requestSaveData;

  @override
  _BaseRequestFormAnOtherRadioState createState() => _BaseRequestFormAnOtherRadioState();
}

class _BaseRequestFormAnOtherRadioState extends State<BaseRequestFormAnOtherRadio> {
  BaseRequestFormProvider _baseRequestFormProvider;

  /// 请求数据
  @override
  Widget build(BuildContext context) {
    _baseRequestFormProvider = BaseRequestFormProvider.of(context);
    return BaseRequestFormContainer(
      showSuffixIcon: false,
      request: widget.request,
      child: buildContent(),
    );
  }

  /// 获取数据
  Widget buildContent() {
    dynamic value = _baseRequestFormProvider.getDataSource(widget.request) ?? widget.request.defaultValue;
    this._setDefaultValue();
    return BaseFormAnOtherRadio(
        radioList: widget.request.selectData,
        selectedKey: value,
        callBack: (item) {
          _baseRequestFormProvider.setDataSource(widget.request, item.key);
          if (widget.requestSaveData != null) widget.requestSaveData(widget.request, _baseRequestFormProvider.getMapDataSource(widget.request));
        });
  }

  void _setDefaultValue() {
    List<String> keys = _baseRequestFormProvider.dataSource.keys.toList();
    String defaultValue = widget.request.defaultValue ?? '';
    if (keys.contains(widget.request.fieldName) == false && defaultValue.length > 0) {
      _baseRequestFormProvider.setDataSource(widget.request, defaultValue);
    }
  }
}
